# 异常处理：是为了提高代码的健壮性，让代码及时遇到了异常，程序不会终止
# try:
# 可能出现错误的代码块
# except:
# 出错之后执行的代码块
# else:
# 没有出错的代码块
# finally:
# 不管有没有出错都执行的代码块
try:
    list1=["python","java","mysql","web"]
    print(list1[2])
    pass
except Exception as e:
    print("下标越界了")
    pass
else:
    print("程序没有发生异常，代码正常执行")
    pass
finally:
    print("无论是否发生异常都会被执行的代码")
# 异常处理的嵌套使用
try:
    num = int(input("请输入一个数字"))
    try:
        result = 15 / num
        pass
    except Exception as msg:
        print(msg)
        print("分母不能为0")
        pass
    pass
except Exception as msg:
    print(msg)
    pass

# 异常的传递
def a():
    print(1/0)
    pass
def b():
    a()
    pass
def c():
    try:
        b()
        pass
    except Exception as e:
        print("分母不能为0异常")
        pass
    else:
        print("没有出现异常")
c()
# 自定义异常，需要继承Exception
class MyException(Exception):
    def __str__(self):
        return "这是一个自定义异常"
    pass
try:
    num=int(input("请输入一个数字"))
    if num>10:
        # 如果数字大于10 ，手动抛出异常
        raise MyException
    pass
except MyException as msg:
    print(msg)
    pass
else:
    print("没有出现异常")